﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Business.BookingManager;

namespace Business
{
    public static class GetFlightInfomation
    {
        private static IBookingManager bookingManager = new BookingManagerClient();
        public static List<Flight> GetFlightStatus(string DEP, string ARR, DateTime startDate, DateTime endDate, string signature)
        {
            GetAvailabilityRequest _request = new GetAvailabilityRequest();
            _request.Signature = signature;
            _request.TripAvailabilityRequest = new TripAvailabilityRequest();
            _request.TripAvailabilityRequest.AvailabilityRequests = new AvailabilityRequest[1];

            AvailabilityRequest _availabilityRequest = new AvailabilityRequest();
            PaxPriceType[] _priceTypes = new PaxPriceType[1];
            _priceTypes[0] = new PaxPriceType();
            _priceTypes[0].PaxType = "ADT";
            _priceTypes[0].PaxDiscountCode=string.Empty;

            _availabilityRequest.PaxPriceTypes = _priceTypes;
            _availabilityRequest.DepartureStation = DEP;
            _availabilityRequest.ArrivalStation = ARR;
          
            if (startDate != endDate)
            {
                _availabilityRequest.BeginDate = startDate;
                _availabilityRequest.EndDate = endDate;
            }
            else
            {
                _availabilityRequest.BeginDate = startDate;
                _availabilityRequest.EndDate = endDate;
            }
            _availabilityRequest.FlightType = FlightType.All;
            _availabilityRequest.CurrencyCode = "VND";
            _availabilityRequest.Dow = DOW.Daily;
            _availabilityRequest.AvailabilityFilter = AvailabilityFilter.Default;
            _availabilityRequest.MaximumConnectingFlights = 2;

            _request.TripAvailabilityRequest.AvailabilityRequests[0] = _availabilityRequest;
            GetAvailabilityResponse response = bookingManager.GetAvailability(_request);

            if (response.GetTripAvailabilityResponse.Schedules.Length <= 0)
                return null;

            List<Flight> flights = new List<Flight>();
            foreach (JourneyDateMarket[] jdms in response.GetTripAvailabilityResponse.Schedules)
            {
                foreach (JourneyDateMarket jdm in jdms)
                {
                    foreach (Journey journey in jdm.Journeys)
                    {
                        foreach (Segment segment in journey.Segments)
                        {
                            Flight _flight = new Flight();
                            _flight.DepartureDate = segment.STD;
                            _flight.DepartureStation = segment.DepartureStation;
                            _flight.ArrivalStation = segment.ArrivalStation;
                            _flight.FlightNo = segment.FlightDesignator.FlightNumber;
                            _flight.FlightStatus = segment.Legs[0].LegInfo.Status.ToString();
                            if (segment.Fares.Length <= 0)
                                _flight.FareAvalable = "NoFares";
                            else
                            {
                                foreach (AvailableFare avail in segment.Fares)
                                {
                                       _flight.FareAvalable += avail.FareBasisCode + "(" + avail.AvailableCount.ToString() + ")  ";
                                   
                                }
                            }

                            flights.Add(_flight);
                        }
                    }
                }
            }
            return flights;
        }
    }
}
